<?php
/**
 * Created by PhpStorm.
 * User: luo
 * Date: 2018/11/1
 * Time: 0:06
 */

namespace app\tools;

use Yii;

class SMS {
  public static function send($mobile, $code){
    header("Accept:application/json;charset=utf-8;");
    header("Content-Type:text/html;charset=utf-8");
    $apikey = "xxxxxxxxxxx";
    $text="【云片网】您的验证码为".$code.'，请于120秒内正确输入，如非本人操作，请忽略此短信。';
    $data=array('text' => $text, 'apikey' => $apikey, 'mobile' => $mobile);
    $ch = curl_init();
    /* 设置验证方式 */
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept:text/plain;charset=utf-8', 'Content-Type:application/x-www-form-urlencoded', 'charset=utf-8'));
    /* 设置返回结果为流 */
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    /* 设置超时时间*/
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    /* 设置通信方式 */
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_URL, 'https://sms.yunpian.com/v2/sms/single_send.json');
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
    $result = json_decode(curl_exec($ch));
    curl_close($ch);
    return $result;
    /*
    * {
        "code": 0,
        "msg": "发送成功",
        "count": 1,
        "fee": 0.05,
        "unit": "RMB",
        "mobile": "13200000000",
        "sid": 3310228982
      }
    */
  }
}